package com.hccake.ballcat.codegen.config;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ccd
 * @Desc
 * @date 2024/12/31 16:31
 */
@Component
public class AsyncScheduledTaskConfig {

	@Bean("myAsync")
	public Executor myAsync() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 最大线程数
		executor.setMaxPoolSize(10);
		// 核心线程数
		executor.setCorePoolSize(5);
		// 任务队列的大小
		executor.setQueueCapacity(50);
		// 线程前缀名
		executor.setThreadNamePrefix("New-Thread-");
		// 线程存活时间
		executor.setKeepAliveSeconds(60);
		// 核心线程空闲时超时终止
		executor.setAllowCoreThreadTimeOut(true);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy() {
		});
		// 线程初始化
		executor.initialize();
		return executor;
	}

}
